package com.pesilo.module.navigate.mvc.view
{
	import com.pesilo.module.navigate.mvc.controller.ApplicationFacade;
	import com.pesilo.module.navigate.mvc.model.WeatherProxy;
	import com.pesilo.module.navigate.mvc.view.component.NavigateComponent;
	
	import flash.events.Event;
	
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	
	public class ApplicationMediator extends Mediator implements IMediator
	{
		public static const NAME:String="ApplicationMediator";
		public function ApplicationMediator(viewComponent:Object)
		{
			super(NAME,viewComponent);
			
			view.addEventListener(Event.ADDED_TO_STAGE,__addedToStageHandle);
		}
		
		protected function __addedToStageHandle(e:Event):void
		{
			onRegister();
		}
		
		override public function onRegister():void{
			if(!facade.retrieveMediator(NavigateMediator.NAME)&&view.navigateCmp){
				facade.registerMediator(new NavigateMediator(view.navigateCmp));
			}
		}
		
		override public function listNotificationInterests():Array{
			return[
				ApplicationFacade.EVENT_SHOW,
				ApplicationFacade.EVENT_HIDE
			];
		}
		
		override public function handleNotification(notification:INotification):void{
			var data:Object=notification.getBody();
			switch(notification.getName()){
				case ApplicationFacade.EVENT_SHOW:
					if(!view.navigateCmp){
						view.navigateCmp=new NavigateComponent();
					}
					view.addChild(view.navigateCmp);
					
					//获取天气信息
					(facade.retrieveProxy(WeatherProxy.NAME)as WeatherProxy).requestData();
					
					break;
				case ApplicationFacade.EVENT_HIDE:
					if(view.contains(view.navigateCmp)){
						view.removeChild(view.navigateCmp);
					}
					break;
			}
		}
		
		protected function get view():App{
			return viewComponent as App;
		}
	}
}